function getCookie(name) {
  if (document.cookie.length > 0) {
    let cStart = document.cookie.indexOf(name + '=')
    if (cStart !== -1) {
      cStart = cStart + name.length + 1
      let cEnd = document.cookie.indexOf(';', cStart)
      if (cEnd === -1) { cEnd = document.cookie.length }
      return decodeURIComponent(document.cookie.substring(cStart, cEnd))
    }
  }
  return ''
}

function setCookie(name, value, time) {
  const exp = new Date()
  exp.setTime(exp.getTime() + time * 1)
  document.cookie = name + '=' + encodeURIComponent(value) + ';expires=' + exp.toGMTString()
}

function delCookie(name) {
  const exp = new Date()
  exp.setTime(exp.getTime() - 1)
  const cval = getCookie(name)
  if (cval != null) {
    document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString()
  }
}

export default {
  getCookie,
  setCookie,
  delCookie
}
